const Common = require('./common')
const WishModel = require('../model/wish')
const Constant = require('../constant/constant')
const dateFormat = require('dateformat')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
let exportObj = {
    list,
    update,
    remove
}
module.exports = exportObj
function list(req,res){
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb)=>{
            Common.checkParams(req.query,['page','rows'],cb);
        },
        query:['checkParams',(results,cb)=>{
            let offset = req.query.rows*(req.query.page - 1)||0;
            let limit = parseInt(req.query.rows)||20;
            let whereCondition = {};
            if(req.query.name){
                whereCondition.name = {[Op.like]:'%'+req.query.name+'%'};
            }
            WishModel.findAndCountAll({
                where:whereCondition,
                offset:offset,
                limit:limit,
                order:[['created_at','DESC']],
            }).then((result)=>{
                let list = [];
                result.rows.forEach((v,i)=>{
                    let obj = {
                        id:v.id,
                        name:v.name,
                        content:v.content,
                        createdAt:dateFormat(v.createdAt,'yyyy-mm-dd HH:MM:ss')
                    };
                    list.push(obj)
                })
                resObj.data = {
                    list,
                    count:result.count
                };
                cb(null)
            }).catch((err)=>{
                console.log(err);
                cb(Constant.DEFAULT_ERROR)
            })
        }]
    };
    Common.autoFn(tasks,res,resObj)
}
function update(req,res){
    console.log(req.body);
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb)=>{
            Common.checkParams(req.body,['id','name','content'],cb);
        },
        update:['checkParams',(results,cb)=>{
           WishModel.update({
               name:req.body.name,
               content:req.body.content
           },{
               where:{
                   id:req.body.id
               }
           }).then((res)=>{
               if(res[0]){
                   cb(null)
               }else{
                   cb(Constant.WISH_NOT_EXSIT)
               }
           }).catch((err)=>{
               console.log(err);
               cb(Constant.DEFAULT_ERROR)
           })
        }]
    };
    Common.autoFn(tasks,res,resObj)
}
function remove(req,res){
    console.log(req.body);
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams:(cb)=>{
            Common.checkParams(req.body,['id'],cb);
        },
        remove:['checkParams',(results,cb)=>{
           WishModel.destroy({
               where:{
                   id:req.body.id
               }
           }).then((res)=>{
               if(res){
                   cb(null)
               }else{
                   cb(Constant.WISH_NOT_EXSIT)
               }
           }).catch((err)=>{
               console.log(err);
               cb(Constant.DEFAULT_ERROR)
           })
        }]
    };
    Common.autoFn(tasks,res,resObj)
}